package com.mjh.partners.controller;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mjh.partners.pojo.Experience;
import com.mjh.partners.pojo.User;
import com.mjh.partners.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONArray;
import sun.misc.BASE64Decoder;


@RequestMapping("/UserController")
@Controller
public class UserController {
  @Autowired
  private UserService userService;
  @RequestMapping("goTest")
  public String goTest() {
    return "test";
  }
  @ResponseBody
  @RequestMapping(value="doLogin",produces = {"application/json;charset=UTF-8"})
  public String doLogin(int userid, String userpwd, HttpServletResponse response, HttpSession session) throws  Exception{
    User user=userService.LoginCheck(userid, userpwd);
    response.setContentType("text/html;charset=utf-8");
    String result="";
    if(user!=null) {
      session.setAttribute("user",user);
      JSONArray json = JSONArray.fromObject(user);  
      result=json.toString();
    }else {
      User user2=new User();
      user2.setUserid(0);
      JSONArray json = JSONArray.fromObject(user2);  
      result=json.toString();
    }
    return result;
  }
  @ResponseBody
  @RequestMapping("doLogout")
  public String doLogout(HttpSession session){
    session.invalidate();
    return "success";
  }

  public String upload(String base64Ex,Date dt,int userid) {
    String reslut = null;
    BASE64Decoder decoder = new BASE64Decoder();  
    try   {
        byte[] b = decoder.decodeBuffer(base64Ex);  
        for(int i=0;i<b.length;++i)  
        {  
            if(b[i]<0)
            {
                b[i]+=256;  
            } 
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String fileName = "b64img__"+sdf.format(dt)+String.valueOf(userid)+".png";
        String imgFilePath = "C:\\picture132\\head\\"+fileName;
        reslut= "http://localhost:8080/Partners/picture132/head/"+fileName;
        OutputStream out = new FileOutputStream(imgFilePath);      
        out.write(b);  
        out.flush();  
        out.close();
        return reslut;
    }catch (Exception e) {
      // TODO: handle exception
      return "error";
    }
    
  }
  @ResponseBody
  @RequestMapping("saveHead")
  public String saveHead(String imagedata,int userid){
    Date dt = new Date();
    User user=new User();
    user.setUserid(userid);
    String userhead=upload(imagedata, dt, userid);
    user.setUserhead(userhead);
    boolean flag=userService.ChangeHead(user);
    if(flag) {
      return "success";
    }else {
      return "erorr";
    }
  }
  @ResponseBody
  @RequestMapping(value="getPartners",produces = {"application/json;charset=UTF-8"})
  public String getPartners(int userid,HttpServletRequest request,HttpServletResponse response) throws  Exception {
    User user=userService.getPartners(userid);
    response.setContentType("text/html;charset=utf-8");
    JSONArray json = JSONArray.fromObject(user);
    String result=json.toString();
    return result;
  }
  @ResponseBody
  @RequestMapping("CheckUserid")
  public String CheckUserid(int userid) {
    User user = userService.CheckUserid(userid);
    if(user==null) {
      return "ok";
    }else {
      return "no";
    }
  }

  @RequestMapping(value="getPartnersNoMe",produces = {"application/json;charset=UTF-8"})
  public String getPartnersNoMe(int userid,Model model) {
    List<User> lists=userService.getPartnersNoMe(userid);
    model.addAttribute("ListNoMe",lists);
    return "zhanshiUser";
  }
  @ResponseBody
  @RequestMapping("registerUser")
  public String registerUser(int userid, String userpwd,String name) {
    User user=new User();
    user.setUserid(userid);
    user.setUserpwd(userpwd);
    user.setName(name);
    boolean flag=userService.registerUser(user);
    if(flag) {
      return "success";
    }else {
      return "error";
    }
  }
  @ResponseBody
  @RequestMapping("updateUser")
  public String registerUser(int userid, String name,String email,String tel) {
    User user=new User();
    user.setUserid(userid);
    user.setName(name);
    user.setEmail(email);
    user.setTel(tel);
    boolean flag=userService.updateUser(user);
    if(flag) {
      return "success";
    }else {
      return "error";
    }
  }
  
}
